//
//  PlayerTableViewController.m
//  ReviewDemo
//
//  Created by 龙 on 2018/5/9.
//  Copyright © 2018年 long. All rights reserved.
//

#define CellIdentifer @"AVPlayerTableViewCell"

#import "PlayerTableViewController.h"
#import "AVPlayerTableViewCell.h"
#import <Masonry.h>
#import "JPVideoPlayerKit.h"

@interface PlayerTableViewController ()
<UITableViewDelegate,UITableViewDataSource,AVPlayerTableViewCellDelegate>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray <NSString *> *dataArray;
@property (nonatomic, strong) AVPlayerTableViewCell *playingCell;

@end

@implementation PlayerTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    if (self.playingCell)
    {
        [self.playingCell.videoView jp_stopPlay];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"高度 ----- %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row]);

    return ScreenWidth;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"将显示 ----- %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row]);

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AVPlayerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.adressLable.text = [NSString stringWithFormat:@"第 %ld 行",indexPath.row];
    cell.delegate = self;
    cell.indexPath = indexPath;
    cell.playButton.hidden = NO;
    
    NSLog(@"已显示 ----- %@ === %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row],cell);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"消失 ----- %@",[NSString stringWithFormat:@"第 %ld 行",indexPath.row]);

    if (!self.playingCell) {
        return;
    }
    if (cell.hash == self.playingCell.hash) {
        [self.playingCell.videoView jp_stopPlay];
        self.playingCell.playButton.hidden = NO;
        self.playingCell = nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - AVPlayerTableViewCellDelegate

- (void)cellPlayButtonDidClick:(AVPlayerTableViewCell *)cell
{
    if (self.playingCell)
    {
        [self.playingCell.videoView jp_stopPlay];
        self.playingCell.playButton.hidden = NO;
    }
    self.playingCell = cell;
    self.playingCell.playButton.hidden = YES;
    self.playingCell.videoView.jp_videoPlayerDelegate = self;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.playingCell.videoView jp_playVideoWithURL:[NSURL URLWithString:self.dataArray[indexPath.row]]
                                     bufferingIndicator:[JPVideoPlayerBufferingIndicator new]
                                            controlView:[[JPVideoPlayerControlView alloc] initWithControlBar:nil blurImage:nil]
                                           progressView:nil
                                configurationCompletion:nil];
}


#pragma mark - setter and getter

- (NSMutableArray<NSString *> *)dataArray
{
    if (_dataArray == nil)
    {
        NSString *videoString = @"http://img.multshows.com/test/testViedo/1967088684R919F";
        _dataArray = [NSMutableArray array];
        
        for (int i = 0; i < 120; i ++)
        {
            [_dataArray addObject:videoString];
        }
    }
    return _dataArray;
}

- (UITableView *)tableView
{
    if (_tableView == nil)
    {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.backgroundColor = COLOR_RGBA(250, 250, 250, 1);
        tableView.rowHeight = ScreenWidth;
        [tableView registerClass:[AVPlayerTableViewCell  class] forCellReuseIdentifier:CellIdentifer];
        tableView.tableFooterView = [UIView new];
        tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 0);
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tableView.separatorColor = CUT_LINE_COLOR;
        
        _tableView = tableView;
    }
    return _tableView;
}

@end
